#include "patternform.h"
#include "ui_patternform.h"
#include <QKeyEvent>
#include <QSerialPort>
#include <QDebug>
#include "uart.h"
#include <QtGlobal>

const char* pattern_text[]={
    "COLORBAR100P",
    "COLORBAR75P",
    "STEPGRAYBAR8",
    "STEPGRAYBAR16",
    "REDSCREEN",
    "GREENSCREEN",
    "BLUESCREEN",
    "CYANSCREEN",
    "MEGNTASCREEN",
    "YELLOWSCREEN",
    "BLACKSCREEN",
    "WHITESCREEN",
    "VER_SPLIT",
    "HOR_SPLIT",
    "VER_MULTIBURST",
    "HOR_MULTIBURST",
    "QUARTER_BLOCK4X4_BF",
    "QUARTER_BLOCK4X4_WF",
    "ALTERNATE_PIXEL",
    "RGBMYRAMPS",
    "BLACKPLUGE",
    "WHITEPLUGE",
    "STILL_GRAYRAMP_BF",
    "STILL_GRAYRAMP_WF",
    "SMPTECOLORBAR",
    "GEOMETRY",
    "WINDOW",
    "BOXES_3D",
    "VER_WHITESCROLL",
    "HOR_WHITESCROLL",
    "AV_SYNC_WB",
    "MGRAYRAMP",
    "MREDRAMP",
    "MGREENRAMP",
    "MBLUERAMP",
    "MOVINGBALL"
};
patternForm::patternForm(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::patternForm)
{
    ui->setupUi(this);
    this->setStyleSheet("background-color: rgb(0, 186, 238);");
    //ui->listWidget->resize(800,300);
    //设置QListWidget的显示模式
    ui->listWidget->setViewMode(QListView::IconMode);
    //设置QListWidget中单元项的图片大小
    ui->listWidget->setIconSize(QSize(160,160));
    //设置QListWidget中单元项的间距
    ui->listWidget->setSpacing(0);
    //设置自动适应布局调整（Adjust适应，Fixed不适应），默认不适应
    ui->listWidget->setResizeMode(QListWidget::Adjust);
    //设置不能移动
    ui->listWidget->setMovement(QListWidget::Static);
#ifdef Q_PROCESSOR_X86
    QDir * imagePath=new QDir(".//image/pattern");
#else
    QDir * imagePath=new QDir("//usr//bin//image//pattern");
#endif
    QStringList filter;
    filter << "*.jpg"<< "*.png"  ;
    QList<QFileInfo> *fileInfo=new QList<QFileInfo>(imagePath->entryInfoList(filter));
    int imageSum = fileInfo->count();//the file have "."and ".." file
//    qDebug()<<imageSum;
    QList<QString>  fileNames;
    fileNames.clear();
    if(imageSum == 0)
    {
        return;
    }
    for(int i=0;i<imageSum;i++)
    {
     fileNames.append(fileInfo->at(i).filePath());
    }
    int i=0;
    for(auto tmp : fileNames)
    {
  //      qDebug()<<tmp<<endl;
        ui->listWidget->addItem(new QListWidgetItem(QIcon(tmp),pattern_text[i++]));
    }
    QImage *img=GetLogo();
    if(img!=NULL)
        ui->label_Logo->setPixmap(QPixmap::fromImage(*img).scaled(ui->label_Logo->size()));

}

patternForm::~patternForm()
{
    delete ui;
}

void patternForm::keyPressEvent(QKeyEvent *event)
{
    int key=event->key();
    if(key==Qt::Key_F12||key==Qt::Key_Exit)
    {
        if(ShowPreWin())
            this->hide();
    }
}
void patternForm::keyReleaseEvent(QKeyEvent *event)
{
    int key=event->key();
  qDebug()<< key;
  if(key==Qt::Key_Space)
  {
      int nPattern_index=ui->listWidget->currentIndex().row();
      SetPattern(nPattern_index);
      qDebug()<<nPattern_index;
      sendUartCmd_pattern(ui->listWidget->currentIndex().row());
  }
  if(key!=Qt::Key_Exit&&key!=Qt::Key_F12)
     if(ShowWin(key,0))
          this->hide();
}

